package com.ray.api.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ray.api.service.UcenterApiService;
import com.ray.auth.system.service.AppService;
import com.ray.auth.system.service.ButtonService;
import com.ray.auth.system.service.DictService;
import com.ray.auth.system.service.MenuService;
import com.ray.auth.system.service.NoticeService;
import com.ray.auth.system.service.OrganizationService;
import com.ray.auth.system.service.RoleService;
import com.ray.auth.system.service.UserService;
import com.ray.auth.system.service.WorkService;
import com.ray.auth.system.table.entity.App;
import com.ray.auth.system.table.entity.Button;
import com.ray.auth.system.table.entity.Dict;
import com.ray.auth.system.table.entity.Menu;
import com.ray.auth.system.table.entity.Notice;
import com.ray.auth.system.table.entity.Organization;
import com.ray.auth.system.table.entity.Role;
import com.ray.auth.system.table.entity.User;
import com.ray.auth.system.table.entity.Work;
import com.xiaoleilu.hutool.lang.Assert;
import com.xiaoleilu.hutool.util.ObjectUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.datastructure.TreeNode;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.base.BaseService;
/**
 * 用户中心
 * @author ray
 *
 */
@Service
public class UcenterApiServiceImpl extends BaseService implements UcenterApiService{
	
	@Autowired
    private UserService userService;
	@Autowired
    private WorkService workService;
	@Autowired
    private NoticeService noticeService;
	@Autowired
	private OrganizationService organizationService;
	@Autowired
	private AppService appService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;
	@Autowired	
	private ButtonService buttonService;
	@Autowired	
	private DictService dictService;
	
	public Result<User> queryUser(String userName) {
		User user = userService.queryUser(userName);
		if(ObjectUtil.isNotNull(user)){
			return new Result<User>(0,null,user);
		}
		return new Result<User>(-1, "无法获取到数据.");
	}

	@Override
	public Result<App> queryApp(String appid) {
		App app = appService.queryApp(appid, null);
		if(ObjectUtil.isNotNull(app)){
			return new Result<App>(0,null,app);
		}
		return new Result<App>(-1, "无法获取到数据.");
	}

	@Override
	public Result<Organization> queryOrgByOrgCode(String orgCode) {
		Organization dept = organizationService.queryOrgByOrgCode(orgCode);
		if(ObjectUtil.isNotNull(dept)){
			return new Result<Organization>(0,null,dept);
		}
		return new Result<Organization>(-1, "无法获取到数据.");
	}

	@Override
	public Result<Organization> queryOrgByTenantId(String tenantId) {
		Organization org = organizationService.queryOrgByTenantId(tenantId);
		if(ObjectUtil.isNotNull(org)){
			return new Result<Organization>(0,null,org);
		}
		return new Result<Organization>(-1, "无法获取到数据.");
	}

	@Override
	public Result<List<Menu>> querySuperUserMenus(String appid) {
		List<Menu> menus = menuService.querySuperUserMenus(appid);
		if(ObjectUtil.isNotNull(menus)){
			return new Result<List<Menu>>(0,null,menus);
		}
		return new Result<List<Menu>>(-1, "无法获取到数据.");
	}

	@Override
	public Result<List<Button>> querySuperUserButtons(String appid) {
		List<Button> buttons = buttonService.querySuperUserButtons(appid);
		if(ObjectUtil.isNotNull(buttons)){
			return new Result<List<Button>>(0,null,buttons);
		}
		return new Result<List<Button>>(-1, "无法获取到数据.");
	}

	@Override
	public Result<List<Menu>> queryUserMenus(String userId, String appid) {
		List<Menu> menus = menuService.queryUserMenus(userId,appid);
		if(ObjectUtil.isNotNull(menus)){
			return new Result<List<Menu>>(0,null,menus);
		}
		return new Result<List<Menu>>(-1, "无法获取到数据.");
	}

	@Override
	public Result<List<Button>> queryUserButtons(String userId, String appid) {
		List<Button> buttons = buttonService.queryUserButtons(userId,appid);
		if(ObjectUtil.isNotNull(buttons)){
			return new Result<List<Button>>(0,null,buttons);
		}
		return new Result<List<Button>>(-1, "无法获取到数据.");
	}

	@Override
	public PageDto<Object> findUserPage(Object object, int pageNum, LoginUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageDto<Object> findRolePage(Object object, int pageNum, LoginUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public PageDto<Object> findOrgPage(Object object, int pageNum, LoginUser user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Result<List<TreeNode>> queryMenus(LoginUser loginUser, String appid) {
		Assert.notNull(loginUser, "用户未登录");
		List<TreeNode> treeNodes = menuService.queryMenuListByParentId(appid, 0, loginUser);
		if(!treeNodes.isEmpty()){
			for(TreeNode node :treeNodes){
				node.setChildren(queryAllmenu(node.getId(), 1, loginUser));
			}
		}
		return new Result<List<TreeNode>>(treeNodes);
	}
    /**
     * 
     * @param appid
     * @param i
     * @param loginUser
     * @return
     */
	private List<TreeNode>  queryAllmenu(String menuId, int level, LoginUser loginUser) {
		List<TreeNode> treeNodes = new ArrayList<>();
		List<TreeNode> qNode = menuService.queryMenuListByParentId(menuId, level, loginUser);
		if(!qNode.isEmpty()){
			for(TreeNode node :qNode){
				node.setChildren(queryAllmenu(node.getId(), level+1, loginUser));
				treeNodes.add(node);
			}
		}
		return treeNodes;
	}

	@Override
	public Result<List<TreeNode>> queryUserHomeMenu(String msgByKey, LoginUser loginUser) {
		List<TreeNode> qNode = menuService.queryUserHomeMenu(msgByKey, loginUser);
		return new Result<List<TreeNode>>(qNode);
	}

	@Override
	public Result<Map<String, List<Button>>> queryUserButtonsWithMenu(LoginUser user, String menuId) {
		if(StrUtil.isBlank(menuId))return new Result<Map<String, List<Button>>>();
		Map<String, List<Button>> buttons = buttonService.queryUserButtonsWithMenu(user, menuId);
		return new Result<Map<String, List<Button>>>(buttons);
	}

	@Override
	public Result<List<TreeNode>> queryOrgListByParentId(String id, Integer level, LoginUser loginUser) {
		 List<TreeNode> list = organizationService.queryOrgListByParentId(id, level, loginUser);
		return new Result<List<TreeNode>>(list);
	}

	@Override
	public Result<Organization> queryOrg(String id) {
		Organization org = organizationService.queryOrg(id, null);
		if(ObjectUtil.isNotNull(org)){
			return new Result<Organization>(0,null,org);
		}
		return new Result<Organization>(-1, "无法获取到数据.");
	}

	@Override
	public String queryOrgName(String id) {
		Organization org = organizationService.queryOrg(id, null);
		if(ObjectUtil.isNotNull(org)){
			return org.getOrgName();
		}
		return null;
	}


	@Override
	public PageDto<User> queryUserList(PageDto<User> pageDto, User user, LoginUser loginUser) {
		return userService.queryUserList(user, pageDto, loginUser);
	}

	@Override
	public Result<List<User>> queryUserList(List<String> ids) {
		List<User> users = userService.queryUser(ids);
		return new Result<List<User>>(0,null,users); 
	}

	@Override
	public PageDto<Role> queryRoleList(PageDto<Role> pageDto, Role role, LoginUser loginUser) {
		return roleService.queryRoleList(role, pageDto, loginUser);
	}

	@Override
	public Result<List<Role>> queryRoleList(List<String> ids) {
		List<Role> roles = roleService.queryRole(ids);
		return new Result<List<Role>>(0,null,roles); 
	}

	@Override
	public List<User> queryUserList(List<String> userIds, List<String> roleIds, LoginUser user) {
		return userService.queryUserList(userIds,roleIds,user);
	}

	@Override
	public String queryUserName(String userId) {
		User user = userService.queryUserByUserId(userId);
		if(ObjectUtil.isNotNull(user)){
			return user.getUserName();
		}
		return null;
	}

	@Override
	public List<Dict> queryDictsByDictType(String type) {
		return dictService.queryDictsByDictType(type);
	}

	@Override
	public List<App> queryApp() {
		return appService.queryUseAppList();
	}

	@Override
	public Work queryWork(String id, LoginUser user) {
		return workService.queryWork(id, user);
	}

	@Override
	public Notice queryNotice(String id, LoginUser user) {
		return noticeService.queryNotice(id, user);
	}

	@Override
	public Notice queryFirstWindowNotice(LoginUser user) {
		return noticeService.queryFirstWindowNotice(user);
	}

	@Override
	public PageDto<Work> queryMyTodoWork(Work work, PageDto<Work> dto, LoginUser loginUser) {
		return workService.queryMyTodoWork(work, dto, loginUser);
	}

	@Override
	public PageDto<Notice> queryNotice(Notice notice, PageDto<Notice> noticePagedto, LoginUser loginUser) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
